package com.control.qct.businesssteward.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.CheckActivity;
import com.control.qct.businesssteward.adapter.CheckOperateAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.CheckDetailModel;
import com.control.qct.businesssteward.model.CheckOperateModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 盘点操作
 * Created by Administrator on 2017/12/11 0011.
 */

@SuppressLint("ValidFragment")
public class CheckOperateFragment extends BaseFragment {
    @BindView(R.id.iv_goods_picture)
    ImageView mIvGoodsPicture;//商品图片
    @BindView(R.id.tv_goods_name)
    TextView mTvGoodsName;//商品名称
    @BindView(R.id.tv_goods_barcode)
    TextView mTvGoodsBarcode;//商品条码
    @BindView(R.id.tv_goods_price)
    TextView mTvGoodsPrice;//商品价格
    @BindView(R.id.tv_goods_type)
    TextView mTvGoodsType;//商品品类
    @BindView(R.id.et_check_practical_num)
    EditText mEtCheckPracticalNum;//盘点数量
    @BindView(R.id.rv_check_content)
    RecyclerView mRvCheckContent;//盘点日志列表
    //    @BindView(R.id.tv_recheck)
//    TextView mTvRecheck;
//    @BindView(R.id.tv_recheck_name)
//    TextView mTvRecheckName;
//    @BindView(R.id.tv_recheck_num)
//    TextView mTvRecheckNum;
//    @BindView(R.id.tv_recheck_time)
//    TextView mTvRecheckTime;
    @BindView(R.id.tv_first_check_sure)
    TextView mTvFirstCheckSure;//初盘
    @BindView(R.id.tv_recheck_sure)
    TextView mTvRecheckSure;//复盘

    private CheckActivity mCheckActivity;
    private List<CheckOperateModel> mCheckOperateModels = new ArrayList<>();
    ;
    private CheckDetailModel.DatasBean mDatasBean;
    private Map<String, String> mParams;
    private String mCheckBatch;
    private CheckOperateAdapter mCheckOperateAdapter;
    private ICheckOperateCallbackListener mICheckOperateCallbackListener;

    public CheckOperateFragment(CheckDetailModel.DatasBean datasBean, String checkBatch,
                                ICheckOperateCallbackListener listener) {
        mDatasBean = datasBean;
        mCheckBatch = checkBatch;
        mICheckOperateCallbackListener = listener;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mCheckActivity = (CheckActivity) context;
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_check_operate, container, false);
    }

    @Override
    protected void initView(View view) {
        activity.setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCheckActivity.getSupportFragmentManager().popBackStack();
            }
        });

        //显示传过来的数据
        GlideUtil.loadImage(this,mDatasBean.getImageBigUrl(), mIvGoodsPicture);
        mTvGoodsName.setText(mDatasBean.getTitle());
        mTvGoodsBarcode.setText(mDatasBean.getBarcode());
        mTvGoodsPrice.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(mDatasBean.getSysPrice()));
        mTvGoodsType.setText(mDatasBean.getCategoryTitle());

        //实例化适配器
        takeStockLogs();
        mRvCheckContent.setLayoutManager(new LinearLayoutManager(activity));
        mCheckOperateAdapter = new CheckOperateAdapter(mCheckOperateModels);
        mRvCheckContent.setAdapter(mCheckOperateAdapter);
    }


    /**
     * 获取盘点日志
     */
    private void takeStockLogs() {
        mParams = new HashMap<>();
        mParams.put("checkBatch", mCheckBatch);
        mParams.put("productCode", mDatasBean.getProductCode());
        mNetProtocol.takeStockLogs(activity, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mCheckOperateModels.clear();
                mCheckOperateModels.addAll((List<CheckOperateModel>) baseBean.getData());
                if (mCheckOperateAdapter != null) {
                    mCheckOperateAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 新增盘点
     */
    private void addTakeStock(int state) {
        if (TextUtils.isEmpty(mEtCheckPracticalNum.getText().toString())) {
            showShortToast("请输入盘点数量！");
            return;
        }
        mParams = new HashMap<>();
        mParams.put("checkBatch", mCheckBatch);
        mParams.put("productCode", mDatasBean.getProductCode());
        mParams.put("barcode", mDatasBean.getBarcode());
        mParams.put("number", mEtCheckPracticalNum.getText().toString());
        mParams.put("state", state + "");
        mNetProtocol.addTakeStock(activity, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mEtCheckPracticalNum.setText("");
                mICheckOperateCallbackListener.result(mCheckBatch);
                showShortToast("操作成功！");
                takeStockLogs();
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });

    }


    @OnClick({R.id.tv_first_check_sure, R.id.tv_recheck_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_first_check_sure://初盘
                addTakeStock(0);
                break;
            case R.id.tv_recheck_sure://复盘
                addTakeStock(1);
                break;
        }
    }

    /**
     * 盘点成功返回刷新列表
     */
    interface ICheckOperateCallbackListener {
        void result(String mBatchNo);
    }
}
